package com.ys.servers.points.data;

import com.ys.servers.common.data.DataEntity;
import com.ys.servers.common.data.UserType;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonObject;

import java.util.List;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by taotao on 2017/5/8.
 */
//@DataObject(generateConverter = false)
public class UserPointsData extends DataEntity {

    //0 扁鹊荟患者，1 扁鹊荟医生
    private UserType userType;

    private long userId;

    //为用户等级预留
    private short userLevel = 0;

    //当前积分
    private int currentPoints = 0;

    //积分累计
    private int totalPoints = 0;

    private long updateTime;

    private Map<String, Integer> totalPointsMap = new HashMap<String, Integer>();

    public int getTotalPoints(String pointsTypeId){
        Integer count = totalPointsMap.get(pointsTypeId);
        if (count == null) {
            return 0;
        } else {
            return count;
        }
    }

    private Map<String, Integer> timelyCountMap = new HashMap<String, Integer>();

    private Map<String, Integer> totalCountMap = new HashMap<>();

    public Map<String, Integer> getTotalCountMap() {
        return totalCountMap;
    }

    public UserPointsData setTotalCountMap(Map<String, Integer> totalCountMap) {
        this.totalCountMap = totalCountMap;
        return this;
    }

    //记录所有已经领完了不能再领的
    private List<String> doneTypesId = new ArrayList();

    public List<String> getDoneTypesId() {
        return doneTypesId;
    }

    public UserPointsData setDoneTypesId(List<String> doneTypesId) {
        this.doneTypesId = doneTypesId;
        return this;
    }

    public UserPointsData addToDoneList(String typeId) {
        if (!isDoneType(typeId))
            doneTypesId.add(typeId);
        return this;
    }

    public boolean isDoneType(String typeId) {
        if (typeId == null) {
            return false;
        }
        return doneTypesId.contains(typeId.trim());
    }

    private Map<String, Long> lastUpdateMap = new HashMap<String, Long>();



   /* @Override
    public String getId() {
        return id;
    }*/

    /**
     * 返回该积分获取类型在当前时间循环内的累计次数
     *
     * @param typeId
     * @return
     */
    public int getTimelyCount(String typeId) {
        Integer count = timelyCountMap.get(typeId);
        if (count == null) {
            return 0;
        } else {
            return count;
        }
    }

    public long getLastUpdate(String typeId) {
        Long time = lastUpdateMap.get(typeId);
        if (time == null) {
            return 0;
        } else {
            return time;
        }
    }

    public UserPointsData doAddPoints(String typeId, int points, boolean resetCount) {
        if (points != 0) {
            long now = System.currentTimeMillis();
            totalPoints += points;
            currentPoints += points;
            lastUpdateMap.put(typeId, now);
            Integer oldPoints = totalPointsMap.get(typeId);
            if (oldPoints == null) {
                totalPointsMap.put(typeId, points);
            } else {
                totalPointsMap.put(typeId, points + oldPoints);
            }
            Integer oldCount = totalCountMap.get(typeId);
            if (oldCount == null) {
                totalCountMap.put(typeId, 1);
            } else {
                totalCountMap.put(typeId, getTimelyCount(typeId) + 1);
            }
            if (resetCount)
                timelyCountMap.put(typeId, 1);
            else
                timelyCountMap.put(typeId, getTimelyCount(typeId) + 1);
            updateTime = now;
        }
        return this;
    }


    public Map<String, Integer> getTotalPointsMap() {
        return totalPointsMap;
    }

    public UserPointsData setTotalPointsMap(Map<String, Integer> totalPointsMap) {
        this.totalPointsMap = totalPointsMap;
        return this;
    }

    public Map<String, Integer> getTimelyCountMap() {
        return timelyCountMap;
    }

    public UserPointsData setTimelyCountMap(Map<String, Integer> timelyCountMap) {
        this.timelyCountMap = timelyCountMap;
        return this;
    }

    public Map<String, Long> getLastUpdateMap() {
        return lastUpdateMap;
    }

    public UserPointsData setLastUpdateMap(Map<String, Long> lastUpdateMap) {
        this.lastUpdateMap = lastUpdateMap;
        return this;
    }

    public UserType getUserType() {
        return userType;
    }

    public UserPointsData setUserType(UserType userType) {
        this.userType = userType;
        return this;
    }

    public long getUserId() {
        return userId;
    }

    public UserPointsData setUserId(long userId) {
        this.userId = userId;
        return this;
    }

    public short getUserLevel() {
        return userLevel;
    }

    public UserPointsData setUserLevel(short userLevel) {
        this.userLevel = userLevel;
        return this;
    }

    public int getCurrentPoints() {
        return currentPoints;
    }

    public UserPointsData setCurrentPoints(int currentPoints) {
        this.currentPoints = currentPoints;
        return this;
    }

    public int getTotalPoints() {
        return totalPoints;
    }

    public UserPointsData setTotalPoints(int totalPoints) {
        this.totalPoints = totalPoints;
        return this;
    }

    public long getUpdateTime() {
        return updateTime;
    }

    public UserPointsData setUpdateTime(long updateTime) {
        this.updateTime = updateTime;
        return this;
    }

    public void resetPointType(String pointsId) {

    }
}
